/********************************************************************************************

Updated to include test coverage for OORDuplicateLeadClosure - Arun Ramachandran(October 11, 2011)

**********************************************************************************************/

@isTest
private class TestMethodCls3 {

    static testMethod void myTest1() {

        // This code runs as the system user

        User u1;

        try{
          u1 = [select Id from User WHERE IsActive=True AND Profile.Name = 'System Administrator'  LIMIT 1];
        } catch (QueryException qe){

        List<User> users = [SELECT Id, Profile.PermissionsModifyAllData FROM User WHERE IsActive = true LIMIT 1000];

        for(User u : users){
            if(u.Profile.PermissionsModifyAllData = true){
              u1 = u;
              break;
        }
        }

        }

        System.debug(u1);

       //*****Create Queue 
       
       Group testGroup = new Group ();
       testGroup.Name = 'TestQueue';
       testGroup.Type = 'Queue';
       insert testGroup;
       
       QueueSObject testQueue = new QueueSObject();
       testQueue.QueueId = testGroup.id;
       testQueue.SObjectType = 'Case';
       insert testQueue;

       // Second Queue       
       Group testGroup2 = new Group ();
       testGroup2.Name = 'TestQueue2';
       testGroup2.Type = 'Queue';
       insert testGroup2;
       
       QueueSObject testQueue2 = new QueueSObject();
       testQueue2.QueueId = testGroup2.id;
       testQueue2.SObjectType = 'Case';
       insert testQueue2;


       test.starttest();
        
        //Run test

        //Assign Case with out any Assignment Groups
        Case c1 = new Case (subject='testcase',tempOwnerID__c=testGroup2.id, OwnerID=u1.id); //tempOwnerID__c=testGroup2.id,  
        insert c1;
        update c1;
        
        
        //Create Assignment Group
        Assignment_Group_Name__c ag1 = new Assignment_Group_Name__c (Name='TestAG', Type__c = 'Case');
        insert ag1;

         
        //Add bad queue name
        Assignment_Group_Queues__c agqBad = new Assignment_Group_Queues__c(name='Bad Queue',Assignment_Group_Name__c = ag1.id );
        //test condition
        try {
            insert agqBad; 
        } catch (DmlException e){
             System.assert(e.getMessage().contains('CUSTOM_VALIDATION_EXCEPTION'), e.getMessage()); 
        
        } //catch

        test.stoptest();
        
    }
    
    static testMethod void myTest2() {

        // This code runs as the system user

        User u1;

        try{
          u1 = [select Id from User WHERE IsActive=True AND Profile.Name = 'System Administrator'  LIMIT 1];
        } catch (QueryException qe){

        List<User> users = [SELECT Id, Profile.PermissionsModifyAllData FROM User WHERE IsActive = true LIMIT 1000];

        for(User u : users){
            if(u.Profile.PermissionsModifyAllData = true){
              u1 = u;
              break;
        }
        }

        }

        System.debug(u1);

       //*****Create Queue 
       
       Group testGroup = new Group ();
       testGroup.Name = 'TestQueue';
       testGroup.Type = 'Queue';
       insert testGroup;
       
       QueueSObject testQueue = new QueueSObject();
       testQueue.QueueId = testGroup.id;
       testQueue.SObjectType = 'Case';
       insert testQueue;

       // Second Queue       
       Group testGroup2 = new Group ();
       testGroup2.Name = 'TestQueue2';
       testGroup2.Type = 'Queue';
       insert testGroup2;
       
       QueueSObject testQueue2 = new QueueSObject();
       testQueue2.QueueId = testGroup2.id;
       testQueue2.SObjectType = 'Case';
       insert testQueue2;


       test.starttest();
        
        //Run test
       
        
        //Create Assignment Group
        Assignment_Group_Name__c ag1 = new Assignment_Group_Name__c (Name='TestAG', Type__c = 'Case');
        insert ag1;        

        //Add Good Queue to Assignment Group
        Assignment_Group_Queues__c agq1 = new Assignment_Group_Queues__c(name=testGroup.Name ,Assignment_Group_Name__c = ag1.id );
        insert agq1;
        
        
        //Add User to Assignment Groups Users
        Assignment_Groups__c agu1 = new Assignment_Groups__c (User__c = u1.id, Active__c='True', Group_Name__c = ag1.id, Last_Assignment__c = datetime.valueOf('2009-01-01 21:13:24') );
        insert agu1;

        Case c2 = new Case (subject='testcase', tempOwnerID__c=testGroup2.id , OwnerID=testGroup.id); //Set owner ID to Queue
        insert c2;
        update c2;        

        test.stoptest();
        
    }

    static testMethod void myTest3() {

        // This code runs as the system user

        User u1;

        try{
          u1 = [select Id from User WHERE IsActive=True AND Profile.Name = 'System Administrator'  LIMIT 1];
        } catch (QueryException qe){

        List<User> users = [SELECT Id, Profile.PermissionsModifyAllData FROM User WHERE IsActive = true LIMIT 1000];

        for(User u : users){
            if(u.Profile.PermissionsModifyAllData = true){
              u1 = u;
              break;
        }
        }

        }

        System.debug(u1);

       //*****Create Queue 
       
       Group testGroup = new Group ();
       testGroup.Name = 'TestQueue';
       testGroup.Type = 'Queue';
       insert testGroup;
       
       QueueSObject testQueue = new QueueSObject();
       testQueue.QueueId = testGroup.id;
       testQueue.SObjectType = 'Case';
       insert testQueue;

       test.starttest();
        
        //Run test        
        
        //Create Assignment Group
        Assignment_Group_Name__c ag1 = new Assignment_Group_Name__c (Name='TestAG', Type__c = 'Case');
        insert ag1;        

        //Add Good Queue to Assignment Group
        Assignment_Group_Queues__c agq1 = new Assignment_Group_Queues__c(name=testGroup.Name ,Assignment_Group_Name__c = ag1.id );
        insert agq1;
        
        
        //Add User to Assignment Groups Users
        Assignment_Groups__c agu1 = new Assignment_Groups__c (User__c = u1.id, Active__c='True', Group_Name__c = ag1.id, Last_Assignment__c = datetime.valueOf('2009-01-01 21:13:24') );
        insert agu1;      
 
        Case c3 = new Case (subject='testcase', OwnerID=testGroup.id); //Set owner ID to Queue
        insert c3;
        update c3;

        test.stoptest();
        
    }

    static testMethod void myTest4() {

        // This code runs as the system user

        User u1;

        try{
          u1 = [select Id from User WHERE IsActive=True AND Profile.Name = 'System Administrator'  LIMIT 1];
        } catch (QueryException qe){

        List<User> users = [SELECT Id, Profile.PermissionsModifyAllData FROM User WHERE IsActive = true LIMIT 1000];

        for(User u : users){
            if(u.Profile.PermissionsModifyAllData = true){
              u1 = u;
              break;
        }
        }

        }

        System.debug(u1);

       //*****Create Queue 
       
       Group testGroup = new Group ();
       testGroup.Name = 'TestQueue';
       testGroup.Type = 'Queue';
       insert testGroup;
       
       QueueSObject testQueue = new QueueSObject();
       testQueue.QueueId = testGroup.id;
       testQueue.SObjectType = 'Case';
       insert testQueue;
      

       test.starttest();
        
        //Run test

        //Create Assignment Group
        Assignment_Group_Name__c ag1 = new Assignment_Group_Name__c (Name='TestAG', Type__c = 'Case');
        insert ag1;        

        //Add Good Queue to Assignment Group
        Assignment_Group_Queues__c agq1 = new Assignment_Group_Queues__c(name=testGroup.Name ,Assignment_Group_Name__c = ag1.id );
        insert agq1;
        
          //Test for AG-Queues already assigned to another Assignment Group
        Assignment_Group_Queues__c agq2 = new Assignment_Group_Queues__c(name=testGroup.Name,Assignment_Group_Name__c = ag1.id );
        try {
            insert agq2;
        } catch (DmlException e){
             System.assert(e.getMessage().contains('CUSTOM_VALIDATION_EXCEPTION'), e.getMessage()); 
        } //catch

        test.stoptest();
        
    }

static testMethod void myTest5() {
         
       Lead a;
                   
        a = new Lead();
        a.FirstName = 'UnitTest1';
        a.LastName = 'UnitTest1';
        a.Company = 'UnitTest1';
        a.Phone = '(415) 111-1111';
        a.Zip_Postal_Code__c = '94555';
        a.LeadSource = 'Internet';
        a.RecordTypeId = GetLocalAdvertiserLeadRecordType();
        insert a;
              
        Lead b;               
              
        b = new Lead();
        b.FirstName = 'UnitTest2';
        b.LastName = 'UnitTest2';
        b.Company = 'UnitTest1';
        b.Phone = '(415) 111-1111';
        b.Zip_Postal_Code__c = '94555';
        b.LeadSource = 'Internet';
        b.RecordTypeId = GetLocalAdvertiserLeadRecordType();
        insert b;
        a.Disposition__c='Signed Contract';
        a.Follow_Up_Date__c= system.now();
        a.Lead_Notes__c='Test Notes';
        Update a;
        
    Database.LeadConvert lc = new database.LeadConvert();
    lc.setLeadId(a.id);
    LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
    lc.setConvertedStatus('Qualified');
    Database.LeadConvertResult lcr = Database.convertLead(lc);
    
  }  
    
static testMethod void myTest6() {
    
    List<User> UserList = new List<User>(); 
    UserList = [select id from user where IsActive = true limit 2];

    Account w = new account ();
    
        w.Name='Winback Testclass';
        w.phone='(415) 131-1111';
        w.Account_Status__c='Inactive';
        w.Type='Win-Back';
        insert w;
    Account w1= New Account();    
        w1.Name='Winback1 Testclass';
        w1.phone='(411) 131-1111';
        w1.Account_Status__c='Inactive';
        w1.Type='Win-Back';
        insert w1;
              
        
    Lead c;
                   
        c = new Lead();
        c.FirstName = 'UnitTest3';
        c.LastName = 'UnitTest3';
        c.Company = 'UnitTest3';
        c.Phone = '(415) 131-1111';
        c.Zip_Postal_Code__c = '95555';
        c.LeadSource = 'Win-Back';
        c.Win_Back_Lead__c = True;
        c.Win_Back_Account__c = w.id;
        c.RecordTypeId = GetLocalAdvertiserLeadRecordType();
        insert c;
        
     Lead d;   
        d = new Lead();
        d.FirstName = 'UnitTest1';
        d.LastName = 'UnitTest1';
        d.Company = 'UnitTest3';
        d.Phone = '(415) 131-1111';
        d.Zip_Postal_Code__c = '95555';
        d.LeadSource = 'Win-Back';
        d.Win_Back_Lead__c = True;
        d.Win_Back_Account__c =w1.id;
        d.RecordTypeId = GetLocalAdvertiserLeadRecordType();
        d.Ownerid=UserList[0].id;
        insert d;

        c.Disposition__c='Signed Contract';
        c.Follow_Up_Date__c= system.now();
        c.Lead_Notes__c='Test Notes';
        //Update c;
        
        
    Database.LeadConvert ld = new database.LeadConvert();
    ld.setLeadId(c.id);
    LeadStatus convertStatus2 = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
    ld.setConvertedStatus('Qualified');
    Database.LeadConvertResult lcr1 = Database.convertLead(ld);

    
    }
    private static string localAdvertiserLeadRecordType;
    private  static ID GetLocalAdvertiserLeadRecordType()
    {       
        if(localAdvertiserLeadRecordType == null)
            localAdvertiserLeadRecordType = RecordTypeHelper.GetRecordTypeID('Lead', 'Local Advertiser');
        
        return localAdvertiserLeadRecordType;
    }
    static testMethod void myTest7() {
    Booking_Dates__c booking = New Booking_Dates__c();
    booking.Booking_Closing_Month__c='3';
    booking.Booking_Closing_Year1__c='2111';
    booking.Booking_Closing_Date__c=Date.ValueOf('2111-4-17');
    Insert booking ;
    Booking_Dates__c booking1 = New Booking_Dates__c();
    booking1.Booking_Closing_Month__c='5';
    booking1.Booking_Closing_Year1__c='2111';
    booking1.Booking_Closing_Date__c=Date.ValueOf('2111-6-17');
    if(booking1.Booking_Closing_Month__c!= booking.Booking_Closing_Month__c && booking1.Booking_Closing_Year1__c!=booking.Booking_Closing_Year1__c){
    
    Insert booking1 ;
    }
    
    }
   
        
}